package com.fun.feifun.modules.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fun.feifun.base.utils.DateUtil;
import com.fun.feifun.base.utils.MD5Util;
import com.fun.feifun.base.utils.common.ListUtil;
import com.fun.feifun.base.utils.format.FormatUtil;
import com.fun.feifun.base.utils.http.HttpUtil;
import com.fun.feifun.modules.business.constant.*;
import com.fun.feifun.modules.business.model.*;
import com.fun.feifun.modules.business.dao.BusinessOrderMapper;
import com.fun.feifun.modules.business.model.dto.BusinessApiOrderInfoDto;
import com.fun.feifun.modules.business.model.dto.BusinessGoodsRouteDto;
import com.fun.feifun.modules.business.model.param.BusinessPlaceOrderParam;
import com.fun.feifun.modules.business.model.param.*;
import com.fun.feifun.modules.business.model.xml.OrderParam;
import com.fun.feifun.modules.business.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fun.feifun.modules.sys.parameter.constant.ParameterConstant;
import com.fun.feifun.modules.sys.parameter.service.ISysParameterService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2024-06-04
 */
@Service
public class BusinessOrderServiceImpl extends ServiceImpl<BusinessOrderMapper, BusinessOrder> implements IBusinessOrderService {

    @Resource
    private IBusinessSupplierService supplierService;
    @Resource
    private IBusinessSupplierGoodsService supplierGoodsService;
    @Resource
    private IBusinessUserRouteService routeService;
    @Resource
    private IBusinessOrderLogService logService;
    @Resource
    private IBusinessGoodsService goodsService;
    @Resource
    private ISysParameterService parameterService;
    @Resource
    private IBusinessUserService userService;
    @Resource
    private IBusinessUserGoodsService userGoodsService;
    @Resource
    private IBusinessAmountRecordService amountRecordService;



    /**
     * 订单匹配供应商
     * @return
     */
    public BusinessOrder matching(BusinessOrder order){
        boolean isSuccess=true;
        BusinessGoodsRouteDto routeDto=null;
        BusinessOrderLog maxIndex = logService.getMaxIndexSupplier(order.getId());
        Integer index=maxIndex.getRouteIndex();
        List<BusinessGoodsRouteDto> goodsRoute = routeService.getGoodsRoute(order.getGoodsId(),order.getBusinessUserId(),null);
        if(ListUtil.isEmpty(goodsRoute)){
            isSuccess=false;
            order.setState(OrderStatusEnum.MateFail.getValue());
            order.setPayState(OrderPayStatusEnum.PayFail.getValue());
            order.setRemake("匹配失败：未找到对应路由供货商，请配置供货商或者配置用户商品路由");
            logService.saveLog(order,OrderStatusEnum.MateFail.getTitle(),index+1);
            return order;
        }
        //全路由都查找完毕失败
        if(index>0&&goodsRoute.size()==maxIndex.getRouteIndex()){
            isSuccess=false;
            order.setState(OrderStatusEnum.MateFail.getValue());
            order.setPayState(OrderPayStatusEnum.PayFail.getValue());
            order.setRemake("订单失败：已匹配最后一个路由都未支付成功。");
            logService.saveLog(order,OrderStatusEnum.MateFail.getTitle(),index+1);
            return order;
        }
        routeDto = goodsRoute.get(maxIndex.getRouteIndex());
        order.setSupplierId(routeDto.getSupplierId());
        order.setSupplierCode(routeDto.getSupplierCode());
        order.setSupplierName(routeDto.getContactName());
        order.setSupplierFees(routeDto.getFeesAmount());
        order.setProductCode(routeDto.getProductCode());

        if(StringUtils.isEmpty(routeDto.getProductCode())){
            isSuccess=false;
            order.setState(OrderStatusEnum.MateFail.getValue());
            order.setRemake("匹配失败：未找到对应的供货商，请配置供货商或者配置用户商品路由");
            logService.saveLog(order,OrderStatusEnum.MateFail.getTitle(),index+1);
            return order;
        }
        if(!routeDto.getUserAuth()){
            isSuccess=false;
            order.setState(OrderStatusEnum.MateFail.getValue());
            order.setRemake("匹配失败：用户未授权此商品");
            logService.saveLog(order,OrderStatusEnum.MateFail.getTitle(),index+1);
            return order;
        }
        if(!routeDto.getSupplierAuth()){
            isSuccess=false;
            order.setState(OrderStatusEnum.MateFail.getValue());
            order.setRemake("匹配失败：["+routeDto.getContactName()+"]供货商未授权此商品");
            logService.saveLog(order,OrderStatusEnum.MateFail.getTitle(),index+1);
            return order;
        }
        if(isSuccess==true){
            if(SupplierTypeEnum.selfSupplier.getValue().equals(routeDto.getSupplierType())){
                order.setState(OrderStatusEnum.WaitPull.getValue());
                order.setRemake("匹配成功：匹配给自供商：["+order.getSupplierName()+"]");
            }else {
                order.setState(OrderStatusEnum.MateSuccess.getValue());
                order.setRemake("匹配成功");
            }
            logService.saveLog(order,OrderStatusEnum.MateSuccess.getTitle(),index+1);
        }
        return order;
    }
    @Override
    public BusinessOrder payment(BusinessOrder order,Boolean isAuto) {
        //设置回调地址
        String returnUrl=parameterService.getParameByCode(ParameterConstant.INTERFACE_RETURN_URL);
        BusinessSupplier supplier = supplierService.getById(order.getSupplierId());
        BusinessGoods goods = goodsService.getById(order.getGoodsId());
        String url=supplier.getInterfacePlaceOrder();
        String key = supplier.getInterfaceSecretKey();
        String userId = supplier.getInterfaceUserId();
        if(StringUtils.isEmpty(userId)){
            order.setState(OrderStatusEnum.PaymentFailed.getValue());
            order.setRemake("供货商下单失败：未设置供货商接口用户id");
            logService.saveLog(order,"供货商下单失败");
            return order;
        }
        if(StringUtils.isEmpty(key)){
            order.setState(OrderStatusEnum.PaymentFailed.getValue());
            order.setRemake("供货商下单失败：未设置密匙");
            logService.saveLog(order,"供货商下单失败：未设置密匙");
            return order;
        }
        if(StringUtils.isEmpty(url)){
            order.setState(OrderStatusEnum.PaymentFailed.getValue());
            order.setRemake("供货商下单失败：未配置下单接口");
            logService.saveLog(order,"供货商下单失败：未配置下单接口");
            return order;
        }
        Map<String, String> responsMap=null;
        OrderParam param=new OrderParam();
        param.setUserid(userId);
        param.setProductid(order.getProductCode());
        param.setPrice(String.valueOf(goods.getAmount().intValue()));
        param.setNum("1");
        param.setMobile(order.getRechargeAccount());
        String spordertime = DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        param.setSpordertime(spordertime);
        param.setSporderid(order.getCode());
        param.setGascardtel(supplier.getContactPhone());
        param.setGascardname(supplier.getContactName());
        param.setSign(param.MD5Sign(key));
        param.setBack_url(returnUrl);
//        param.setPaytype("111");
        try {
            String responseBody = HttpUtil.odPost(url, param);
            responsMap = FormatUtil.convertXmlToMap(responseBody);
            order.setState(OrderStatusEnum.Payment.getValue());
            if(responsMap.containsKey("resultno")&&responsMap.get("resultno").equals(ReturnStatusEnum.wait.getValue())){
                order.setState(OrderStatusEnum.PaymentSuccess.getValue());
                order.setOutsideOrderCode(responsMap.get("orderid"));
                order.setPayTime(LocalDateTime.now());
                logService.saveLog(order,"供货商下单成功");
            }else {
                order.setState(OrderStatusEnum.PaymentFailed.getValue());
                order.setPayTime(null);
                String remake = responsMap.get("remark1");
                order.setRemake(remake);
                logService.saveLog(order,remake);
            }
            if (responsMap.containsKey("remark1")){
                String remake = responsMap.get("remark1");
                order.setRemake(remake);
            }
        }catch (Exception e){
            e.printStackTrace();
            order.setState(OrderStatusEnum.PaymentFailed.getValue());
            order.setRemake("下单失败");
        }
        this.updateById(order);
        return order;
    }
    @Override
    @Transactional
    public BusinessOrder paymentReturn(String outsideOrderCode,String resultno) {
        QueryWrapper<BusinessOrder> orderIdWrapper = new QueryWrapper<>();
        orderIdWrapper.lambda().eq(BusinessOrder::getOutsideOrderCode,outsideOrderCode);
        BusinessOrder order = this.getOne(orderIdWrapper);
        if(order==null){
            return null;
        }
        String remake="";
        if(InterfaceConstant.returnSuccess.equals(resultno)){
            order.setState(OrderStatusEnum.PaySuccess.getValue());
            order.setPayState(OrderPayStatusEnum.PaySuccess.getValue());
            remake="充值成功";
            order.setReturnTime(LocalDateTime.now());
        }
        if(InterfaceConstant.returnerr.equals(resultno)){
            order.setState(OrderStatusEnum.PayFail.getValue());
            order.setPayState(OrderPayStatusEnum.PayFail.getValue());
            remake= "充值失败";
            order.setReturnTime(null);
        }
        order.setRemake(remake);
        boolean b = updateById(order);
        logService.saveLog(order,remake);
        //扣钱
        Long userId = order.getBusinessUserId();
        if(!userId.equals(0l)){
            BusinessUser user = userService.getById(userId);
            BigDecimal amount = user.getAmount();
            BigDecimal cost = order.getGoodsAmount().add(order.getFeesAmount());
            BigDecimal last = amount.subtract(cost);
            BusinessAmountRecord record=new BusinessAmountRecord();
            record.setUserId(user.getId());
            record.setAmount(cost);
            record.setBeforeAmount(BigDecimal.ZERO.subtract(amount));
            record.setAfterAmount(last);
            record.setRemake("充值手机号{"+order.getRechargeAccount()+"}"+order.getGoodsName()+"成功,电话费["+order.getGoodsAmount()+"元],手续费["+order.getFeesAmount()+"元]");
            amountRecordService.save(record);
            user.setAmount(last);
            user.setLockAmount(user.getLockAmount().subtract(cost));
            userService.updateById(user);
        }
        this.updateById(order);
        return order;
    }

    @Override
    public Map<String, String> getInterfaceOrederInfo(BusinessOrder  order) {
        String url = "http://121.37.177.158:9086/searchpay.do";
        BusinessSupplier supplier = supplierService.getById(order.getSupplierId());
        String orderCode = order.getCode();
        String userId = supplier.getInterfaceUserId();
        String key = supplier.getInterfaceSecretKey();
        try {
            StringBuilder signStr = new StringBuilder();
            signStr.append("userid=" + userId + "&");
            signStr.append("sporderid=" + orderCode + "&");
            signStr.append("key=" + key);
            String sign = MD5Util.encrypt(signStr.toString());
            Map<String, String> param = new HashMap<>();
            param.put("userid", userId);
            param.put("sporderid", orderCode);
            param.put("sign", sign);
            String responseBody = HttpUtil.odPost(url, param);
            Map<String, String> map = FormatUtil.convertXmlToMap(responseBody);
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void orderOperate(BusinessOrderOperation param) {
        QueryWrapper<BusinessOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().in(BusinessOrder::getId,param.getIds());
        List<BusinessOrder> list = this.list(queryWrapper);
        String remake="";
        for (BusinessOrder order:list) {
            if(order.getPayState()!=OrderPayStatusEnum.payment.getValue()){
                continue;
            }
            if(param.getOperation()){
                order.setState(OrderStatusEnum.PaySuccess.getValue());
                order.setPayState(OrderPayStatusEnum.PaySuccess.getValue());
                remake="手动成功";
                order.setReturnTime(LocalDateTime.now());
            }else {
                order.setState(OrderStatusEnum.PayFail.getValue());
                order.setPayState(OrderPayStatusEnum.PayFail.getValue());
                remake= "手动失败";
                order.setReturnTime(null);
            }
            order.setRemake(remake);
            logService.saveLog(order,remake);
            boolean b = updateById(order);
            //手动成功和失败需要扣钱不
        }
    }


    @Override
    public BusinessOrder placeOrder(BusinessGoods goods, BusinessUser user, BusinessPlaceOrderParam item){
        String remake="";
        QueryWrapper<BusinessUserGoods> userGoodsWrapper=new QueryWrapper<>();
        userGoodsWrapper.lambda()
                .eq(BusinessUserGoods::getGoodsId,goods.getId())
                .eq(BusinessUserGoods::getUserId,user.getId());
        BusinessUserGoods businessGoods = userGoodsService.getOne(userGoodsWrapper);
        if(ObjectUtils.isEmpty(businessGoods)){
            remake="代理未代理这个商品，请联系管理员设置";
        }
        //可用额度
        BigDecimal availableAmount = user.getAmount().add(user.getCreditAmount()).subtract(user.getLockAmount());
        //总花费
        BigDecimal totalAmount = businessGoods.getGoodsAmount().add(businessGoods.getFeesAmount());
        if(availableAmount.subtract(totalAmount).compareTo(BigDecimal.ZERO)<0){
            remake=remake+"||代理用户，金额或信用额不足，请联系管理员加款";
        }
        BusinessOrder order=new BusinessOrder();
        order.init(goods,user,item,businessGoods.getFeesAmount());
        if(!StringUtils.isEmpty(remake)){
            order.setState(OrderStatusEnum.initFail.getValue());
            order.setPayState(OrderPayStatusEnum.payment.getValue());
            this.save(order);
            logService.saveLog(order,OrderMsgConstant.log_user_place_order_fail);
            throw new RuntimeException(remake);
        }
        if(this.save(order)){
            //订单锁定金额
            user.setLockAmount(user.getLockAmount().add(totalAmount));
            userService.updateById(user);
//            save(order);
            order = this.reRouteOrder(order);
            //支付失败释放锁定金额。
            if(order.getPayState()==OrderPayStatusEnum.PayFail.getValue()){
                this.returnMsgToUrl(order);
                user.setLockAmount(user.getLockAmount().subtract(totalAmount));
                userService.updateById(user);
            }
        }
        this.updateById(order);
        return order;
    }

    @Override
    public  Map<String,Object>pullOrder(BusinessOrderOutPutParam param){
        String nowSign = param.MD5Sign();
//        if(!nowSign.equals(param.getSign())){
//            throw new RuntimeException("加密密匙不一致");
//        }
        QueryWrapper<BusinessSupplier> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(BusinessSupplier::getCode,param.getSupplierCode());
        BusinessSupplier supplier = supplierService.getOne(wrapper);
        if(supplier==null){
            throw new RuntimeException("供货商编码不存在");
        }
        if(!SupplierTypeEnum.selfSupplier.getValue().equals(String.valueOf(supplier.getType()))){
            throw new RuntimeException("供货商不属于自供货商类型");
        }
        QueryWrapper<BusinessOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessOrder::getSupplierId,supplier.getId()).eq(BusinessOrder::getState, OrderStatusEnum.WaitPull.getValue());
        BusinessOrder order= this.getOne(queryWrapper);
        if(order==null){
            throw new RuntimeException("没有可拉去的订单");
        }
        order.setState(OrderStatusEnum.AlreadyPull.getValue());
        order.setRemake("订单已经被["+supplier.getContactName()+"]拉取");
        logService.saveLog(order,order.getRemake());
        this.updateById(order);
        Map<String,Object> reMap=new HashMap<>();
        reMap.put("orderCode",order.getCode());
        reMap.put("goodsCode",order.getGoodsCode());
        reMap.put("goodsName",order.getGoodsName());
        reMap.put("goodsAmount",order.getGoodsAmount());
        reMap.put("productCode",order.getProductCode());
        reMap.put("rechargeAccount",order.getRechargeAccount());
        return reMap;
    }

    @Override
    public boolean payPullOrder(BusinessOrderSuccessParam param){
        String nowSign = param.MD5Sign();
//        if(!nowSign.equals(param.getSign())){
//            throw new RuntimeException("加密密匙不一致");
//        }
        QueryWrapper<BusinessSupplier> wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(BusinessSupplier::getCode,param.getSupplierCode());
        BusinessSupplier supplier = supplierService.getOne(wrapper);
        if(supplier==null){
            throw new RuntimeException("供货商编码不存在");
        }
        if(!SupplierTypeEnum.selfSupplier.getValue().equals(String.valueOf(supplier.getType()))){
            throw new RuntimeException("供货商不属于自供货商类型");
        }
        QueryWrapper<BusinessOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessOrder::getSupplierId,supplier.getId())
                .eq(BusinessOrder::getState, OrderStatusEnum.AlreadyPull.getValue())
                .eq(BusinessOrder::getCode,param.getOrderCode());
        BusinessOrder order= this.getOne(queryWrapper);
        if(order==null){
            throw new RuntimeException("订单不存在");
        }
        if(param.getState().equals(1)){//成功
            order.setState(OrderStatusEnum.PaySuccess.getValue());
            order.setPayState(OrderPayStatusEnum.PaySuccess.getValue());
            order.setReturnTime(LocalDateTime.now());
            order.setRemake("订单被["+supplier.getContactName()+"]充值成功");
        }else{
            order.setState(OrderStatusEnum.PayFail.getValue());
            order.setPayState(OrderPayStatusEnum.PayFail.getValue());
            order.setRemake("订单被["+supplier.getContactName()+"]充值失败");
        }
        logService.saveLog(order,order.getRemake());
        order.setOutsideOrderCode(param.getOutsideOrderCode());
        //信息返回给用户
        this.returnMsgToUrl(order);
        return true;
    }

    /**
     * 重新路由和支付
     * @param order
     * @return
     */
    @Override
    @Async("threadPoolTaskExecutor")
    public BusinessOrder reRouteOrder(BusinessOrder order){
        //自动充值
        String autoPay = parameterService.getParameByCode(ParameterConstant.PLACE_ORDER_PAY);
        //自动匹配
        String autoMatch = parameterService.getParameByCode(ParameterConstant.PLACE_ORDER_MATCHING);
        //写入订单日志
        logService.saveLog(order,OrderMsgConstant.log_user_place_order_success,0);
        do{
            do {
                if(ParameterConstant.PLACE_ORDER_MATCHING_TURE.equals(autoMatch)){
                    this.matching(order);
                }
            } while (order.getState()==OrderStatusEnum.MateFail.getValue()&&OrderPayStatusEnum.PayFail.getValue()!=order.getPayState());
            //匹配成功  开始支付
            if(order.getState()==OrderStatusEnum.MateSuccess.getValue()){
                //申请支付
                if(ParameterConstant.PLACE_ORDER_MATCHING_TURE.equals(autoPay)){
                    order = this.payment(order, true);
                }
            }
        }while (order.getState()==OrderStatusEnum.PaymentFailed.getValue()&&OrderPayStatusEnum.PayFail.getValue()!=order.getPayState());
        updateById(order);
        return order;
    }
    /**
     *返回订单信息给用户
     * @author wukefei
     * @date 2024/9/5 17:41
     */
    @Override
    public void backUserOrderInfo(){
        //查询那些订单还在支付中
        QueryWrapper<BusinessOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .in(BusinessOrder::getPayState, Arrays.asList(OrderPayStatusEnum.PayFail.getValue(),OrderPayStatusEnum.PaySuccess.getValue()))
                .eq(BusinessOrder::getIsBack,0);
        List<BusinessOrder> list = this.list(queryWrapper);
        if(list.isEmpty()){
            return;
        }
        List<BusinessOrder> saveList=new ArrayList<>();
        for (BusinessOrder order : list) {
            saveList.add(this.returnMsgToUrl(order));
        }
//        this.updateBatchById(saveList);
    }


    public BusinessOrder returnMsgToUrl(BusinessOrder order)  {
        BusinessUser user = userService.getById(order.getBusinessUserId());
        String backUrl = order.getBackUrl();
        Map<String, String> responsMap=null;
        if(StringUtils.isEmpty(backUrl)){
            order.setIsBack(-1);
            order.setBackMsg("用户没有传入回调地址");
            return order;
        }
        try {
//            Map<String,Object> param=new HashMap<>();
//            param.put("userid",order.getBusinessUserCode());
//            param.put("orderid",order.getCode());
//            param.put("sporderid",order.getUserOrderCode());
//            String yyyyMMddHHmmss = DateUtil.getNowDate("yyyyMMddHHmmss");
//            param.put("merchantsubmittime",yyyyMMddHHmmss);
//            String resultno =order.getPayState()==OrderPayStatusEnum.PaySuccess.getValue()?"1":"9";
//            param.put("resultno",resultno);
//            StringBuilder signStr = new StringBuilder();
//            signStr.append("userid="+order.getBusinessUserCode()+"&&");
//            signStr.append("orderid="+order.getCode()+"&&");
//            signStr.append("sporderid="+order.getUserOrderCode()+"&&");
//            signStr.append("merchantsubmittime="+yyyyMMddHHmmss+"&&");
//            signStr.append("resultno="+resultno+"&&");
//            signStr.append("key="+user.getSecretKey());
//            String sign = MD5Util.encrypt(signStr.toString());
//            param.put("sign",sign);
//            param.put("parvalue",order.getGoodsAmount());
//            param.put("remark1",order.getRemake());
//            param.put("fundbalance",user.getAmount());
            BusinessApiOrderInfoDto param=new BusinessApiOrderInfoDto();
            param.setOrderCode(order.getCode());
            param.setRechargeAccount(order.getRechargeAccount());
            param.setGoodsCode(order.getGoodsCode());
            param.setGoodsName(order.getGoodsName());
            param.setGoodsAmount(order.getGoodsAmount());
            param.setBusinessUserCode(order.getBusinessUserCode());
            param.setBusinessUserNike(order.getBusinessUserNike());
            param.setPayState(order.getPayState());
            param.setRemake(order.getRemake());
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            param.setPayTime(order.getCreateTime().format(formatter));

            String responseBody = HttpUtil.odPost(backUrl, param,null);
            if(responseBody.equals("ok")){
                order.setIsBack(1);
                order.setBackMsg("返回用户信息成功："+FormatUtil.objToJson(param));
            }else {
                order.setIsBack(-1);
                order.setBackMsg("返回用户信息失败："+FormatUtil.objToJson(param));
            }
        }catch (Exception e){
            e.printStackTrace();
            order.setIsBack(-1);
            order.setBackMsg("返回用户信息失败：系统异常");
        }
        this.updateById(order);
        return order;
    }

    /**
     *查询供货商订单支付情况
     * @author wukefei
     * @date 2024/9/5 17:41
     */
    @Override
    public void reFreshOrderInfo(){
        //查询那些订单还在支付中
        QueryWrapper<BusinessOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessOrder::getState, OrderStatusEnum.PaymentSuccess.getValue());
        List<BusinessOrder> list = this.list(queryWrapper);
        if(list.isEmpty()){
            return;
        }
        System.out.println("执行任务查询到["+list.size()+"]条数据: " + System.currentTimeMillis());
        //订单状态
        for (BusinessOrder order : list) {
            Map<String, String> info = this.getInterfaceOrederInfo(order);
            if(info!=null){
                String resultno = info.get("resultno");
                this.paymentReturn(String.valueOf(order.getCode()),resultno);
            }
        }
    }

    /**
     *查询供货商订单支付情况
     * @author wukefei
     * @date 2024/9/5 17:41
     */
    @Override
    public Page<BusinessOrderSummary> summaryPage(BusinessSummaryPage param){
        Integer summaryCount = this.baseMapper.getSummaryCount(param);
        List<BusinessOrderSummary> list = this.baseMapper.getSummaryList(param);
        list.forEach(it->it.setTypeTitle(param.getType().equals(1)?"代理用户":"供货商"));
        Page<BusinessOrderSummary> page = new Page(param.getPage(),param.getSize());
        page.setRecords(list);
        page.setTotal(summaryCount);
        return page;
    }
}
